package com.marketingexpert.controller.personmgnt;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.config.Configuration;
import com.marketingexpert.config.SystemErrorCode;
import com.marketingexpert.dao.AuthorityDao;
import com.marketingexpert.domain.User;
import com.marketingexpert.domain.permission.Permission;
import com.marketingexpert.domain.permission.Role;
import com.marketingexpert.domain.permission.RolePermission;
import com.marketingexpert.domain.permission.UserRole;
import com.marketingexpert.exception.MarketingExpertException;
import com.marketingexpert.service.usermgnt.UserService;
import com.marketingexpert.util.Resources;

public class GetMyAuthorityInfo implements Controller {

	private UserService userService;

	private AuthorityDao authorityDao;

	private String successView;

	private SystemErrorCode systemErrorCode;

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		try {

			if (request.getSession().getAttribute("parentUserId") == null) {
				request.getSession().removeAttribute("parentUserId");
				request.getSession().removeAttribute("taobaoSessionKey");
				throw new MarketingExpertException(systemErrorCode
						.getSystemSessionExpired(), Resources
						.getString("SystemSessionExpired"));
			}

			String parentUserId = (String) request.getSession().getAttribute(
					"parentUserId");

			User myUser = getUserService().queryUserByUId(parentUserId);

			UserRole _ur = getAuthorityDao().getUserRole(parentUserId);
			Role role = getAuthorityDao().getRole(_ur.getRoleId());
			role.setRoleName(Resources.getString(role.getRoleIdentify()));
			String userType = role.getRoleName();
			String dealine = myUser.getEndLine();

			List<String> userableApp = new ArrayList<String>();
			List<RolePermission> rolePermissions = role.getRolepermissions();
			for (RolePermission _rp : rolePermissions) {

				Permission p = _rp.getPermission();

				if (p.getOperation().getOperationName().equals("use")) {
					userableApp.add(Resources.getString(p.getResource()
							.getBeanName()));
				}
			}

			Map<String, Object> params = new HashMap<String, Object>();

			params.put("userType", userType);
			params.put("dealine", dealine);
			params.put("userableApp", userableApp);

			return new ModelAndView(getSuccessView(), "params", params);
		} catch (MarketingExpertException e) {
			if (e.getErrCode()
					.equals(systemErrorCode.getSystemSessionExpired())) {
				return new ModelAndView(getSuccessView(), "sessionError", e
						.getMessage());
			} else if (e.getErrCode().equals(systemErrorCode.getUserNotExist())) {
				return new ModelAndView(getSuccessView(), "userNotExist", e
						.getMessage());
			} else {
				return new ModelAndView(getSuccessView(),
						"queryAutorityFailed", e.getMessage());
			}
		}
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public AuthorityDao getAuthorityDao() {
		return authorityDao;
	}

	public void setAuthorityDao(AuthorityDao authorityDao) {
		this.authorityDao = authorityDao;
	}

	public String getSuccessView() {
		return successView;
	}

	public void setSuccessView(String successView) {
		this.successView = successView;
	}

	public SystemErrorCode getSystemErrorCode() {
		return systemErrorCode;
	}

	public void setSystemErrorCode(SystemErrorCode systemErrorCode) {
		this.systemErrorCode = systemErrorCode;
	}
}
